﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Filter;
using System.Linq.Expressions;
using COFE.Data.Utils.ThirdParty;

namespace COFE.Data.Filter
{
    public class FileTypeEntryFilterExpression :
        EntryFilterExpression<COFE.Core.Filter.FileTypeEntryFilter>
    {
        public FileTypeEntryFilterExpression()
            : base()
        {

        }

        public override Expression<Func<Entry, bool>> getExpression(
           IEntryFilter f, IOptionFilter[] options)
        {
            var entryFilter = f as COFE.Core.Filter.FileTypeEntryFilter;
            string type = entryFilter.Value.ToLower();
            Expression<Func<Entry, bool>> exp;
            switch (type)
            {
                case FileTypeEntryFilter.File: 
                    exp = (e) => e is FileEntry;
                    break;
                case FileTypeEntryFilter.Folder:
                    exp = (e) => e is FolderEntry || (e as FileEntry).IsFileBasedFS;
                    break;
                default:
                    exp = (e) => (e is FileEntry) &&
                         e.ParsePath.ToLower().EndsWith("." + type);
                    break;
            }
            if (f.Negative)
                return exp.Not();
            return exp;
        }
       
       

    }
}
